﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyPoints.Model;

namespace MyPoints.UserControls
{
  /// <summary>
  /// Interaction logic for SelectFile.xaml
  /// </summary>
  public partial class SelectFile : UserControl
  {
    public enum eMode
    {
      Folder,
      File
    }

    public static readonly DependencyProperty SelectedProperty =
        DependencyProperty.Register("Selected", typeof(string), typeof(SelectFile));
    public static readonly DependencyProperty ModeProperty =
      DependencyProperty.Register("Mode", typeof(eMode), typeof(SelectFile));
    public static readonly DependencyProperty FilterProperty =
      DependencyProperty.Register("Filter", typeof(string), typeof(SelectFile));


    public string Selected
    {
      get { return (string)GetValue(SelectedProperty); }
      set { SetValue(SelectedProperty, value); }
    }
    public eMode Mode
    {
      get { return (eMode)GetValue(ModeProperty); }
      set { SetValue(ModeProperty, value); }
    }
    public string Filter
    {
      get { return (string)GetValue(FilterProperty); }
      set { SetValue(FilterProperty, value); }
    }

    public SelectFile()
    {
      InitializeComponent();
      this.Filter = "All files (*.*)|*.*";
    }

    private void btnBrowse_Click(object sender, RoutedEventArgs e)
    {
      switch (this.Mode)
      {
        case eMode.File:
          UpdateFile();
          break;
        case eMode.Folder:
          UpdateFolder();
          break;
        default:
          throw new NotSupportedException();
      }
    }

    private void UpdateFolder()
    {
      System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
      if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
        this.Selected = dlg.SelectedPath;
      }
    }

    private void UpdateFile()
    {
      Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
      dlg.Filter = this.Filter;
      Nullable<bool> result = dlg.ShowDialog();
      if (result == true)
      {
        this.Selected = dlg.FileName;
      }
    }
  }
}